package com.campus.controller;

import com.campus.common.config.CodeConfig;
import com.campus.entity.ResultBody;
import com.campus.entity.RoomManager;
import com.campus.entity.vo.RoomManagerVO;
import com.campus.service.RoomManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName RoomManagerController
 * @Description TODO
 * @Author AlaskaBoo
 * @Date 2023/4/14 12:32
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/roomManagerInfo")
public class RoomManagerController {
    @Autowired
    private RoomManagerService roomManagerService;

    /**
     * 添加或者修改
     *
     * @param roomManager
     * @return
     */
    @PostMapping
    @CacheEvict(value = "roomManagerInfo", allEntries = true)
    public ResultBody save(@RequestBody RoomManager roomManager) {
        if (roomManager.getId() == null || roomManager.getId() == -1) {
            roomManagerService.save(roomManager);
        } else {
            roomManagerService.updateById(roomManager);
        }
        return ResultBody.ok().message(CodeConfig.SUCCESS.message).data("roomManager", roomManager);
    }

    /**
     * 根据宿管ID查询
     *
     * @param account
     * @return
     */
    @GetMapping("/{account}")
    @Cacheable(value = "roomManagerInfo", key = "#account")
    public ResultBody selectRoomManagerById(@PathVariable String account) {
        RoomManager roomManager = roomManagerService.lambdaQuery().in(RoomManager::getAccount, account).one();
        RoomManagerVO roomManagerVO = new RoomManagerVO();
        BeanUtils.copyProperties(roomManager, roomManagerVO);
        return ResultBody.ok().data("roomManagerInfo", roomManagerVO);
    }

    /**
     * 根据宿管ID删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @CacheEvict(value = "roomManagerInfo", allEntries = true)
    public ResultBody deleteRoomManager(@PathVariable Long id) {
        roomManagerService.removeById(id);
        return ResultBody.ok().data("id", id).message(CodeConfig.DELETE_USER.message).describe(CodeConfig.DELETE_USER.message);
    }
}
